import Vue from 'vue'
import Router from 'vue-router'
// 进入知乎登入注册页面
import Enter from '../pages/EnterPage'
import LoginBox from '../pages/enterPage/LoginBox'
import RegisterBox from '../pages/enterPage/RegisterBox'
// 知乎首页页面
import HomePage from '../pages/HomePage'
import HomeRecommend from '../pages/homePage/HomeRecommend'
import HomeFollow from '../pages/homePage/HomeFollow'
import HomeHotList from '../pages/homePage/HomeHotList'
// 知乎首页的写文章(问题)页面
import WriteQuestionPage from '../pages/WriteQuestionPage'
// 知乎发现页面
import FindPage from '../pages/FindPage'
// 知乎话题页面
import TalkPage from '../pages/TalkPage'
// 知乎搜索页面
import SearchPage from '../pages/SearchPage'
// 知乎个人页面
import UserPage from '../pages/UserPage'
import UserDynamic from '../pages/userPage/UserDynamic'
import UserArticle from '../pages/userPage/UserArticle'
import UserFollow from '../pages/userPage/UserFollow'
import UserEdit from '../pages/userPage/UserEdit'
import UserProblem from '../pages/userPage/UserProblem'
import UserTopic from '../pages/userPage/UserTopic'
import UserFollowing from '../pages/userPage/UserFollowing'
import UserFollowed from '../pages/userPage/UserFollowed'
// 知乎通知页面
import NoticePage from '../pages/NoticePage'
// 知乎私信页面
import PrivateLetterPage from '../pages/LetterPage'
import LetterView from '../pages/LetterPage/LetterView'
// 知乎首页推荐里面文章页面
import HomeArticlePage from '../pages/HomeArticlePage'
// 测试页面
import Test from '../components/Test1'
// 后台管理界面
// 后台管理登入注册页
import AdminEnter from '../pages/adminPage/Enter'
import AdminLoginBox from '../pages/adminPage/enter/LoginBox'
import AdminRegisterBox from '../pages/adminPage/enter/RegisterBox'
import AdminForgetBox from '../pages/adminPage/enter/ForgetBox'
// 功能结构页
import AdminHomes from '../pages/adminPage/Homes'
import AdminHomePage from '../pages/adminPage/homes/HomePage'
import xxxPage1 from '../pages/adminPage/homes/xxxPage1'
import Function1 from '../pages/adminPage/homes/xxxpage1/Function1'
import Function2 from '../pages/adminPage/homes/xxxpage2/Function2'
import Function3 from '../pages/adminPage/homes/xxxpage1/Function3'
import Function4 from '../pages/adminPage/homes/xxxpage1/Function4'
import xxxPage2 from '../pages/adminPage/homes/xxxPage2'
import xxxPage3 from '../pages/adminPage/homes/xxxPage3'
import xxxPage4 from '../pages/adminPage/homes/xxxPage4'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/ZhiHu'
    },
    {// 进入知乎登入注册页
      path: '/ZhiHu',
      name: 'ZhiHu',
      component: Enter,
      children: [
        {// 登入框
          path: '/login',
          name: 'LoginBox',
          component: LoginBox
        },
        {// 注册框
          path: '/register',
          name: 'RegisterBox',
          component: RegisterBox
        }
      ],
      redirect: '/login'// 默认先显示登入框
    },
    {// 知乎首页页面
      path: '/home',
      name: 'HomePage',
      component: HomePage,
      meta: {auth: true},
      children: [
        {// 首页里面的 推荐页面
          path: '/home/recommend',
          name: 'HomeRecommend',
          component: HomeRecommend,
          meta: {auth: true}
        },
        {// 首页里面的 关注页面
          path: '/home/follow',
          name: 'HomeFollow',
          component: HomeFollow,
          meta: {auth: true}
        },
        {// 首页里面的 热榜页面
          path: '/home/hotList',
          name: 'HomeHotList',
          component: HomeHotList,
          meta: {auth: true}
        }
      ],
      redirect: '/home/recommend'
    },
    {// 知乎首页写文章(问题)页面
      path: '/writeQuestion',
      name: 'WriteQuestionPage',
      component: WriteQuestionPage,
      meta: {auth: true}
    },
    {// 知乎发现页面
      path: '/find',
      name: 'Find',
      component: FindPage,
      meta: {auth: true}
    },
    {// 知乎话题页面
      path: '/talk/:typeName',
      name: 'Talk',
      component: TalkPage,
      meta: {auth: true}
    },
    {// 知乎搜索页面
      path: '/search',
      name: 'SearchPage',
      component: SearchPage,
      meta: {auth: true}
    },
    {// 知乎用户页面
      path: '/user',
      name: 'UserPage',
      component: UserPage,
      children: [
        {// 用户的动态页面
          path: '/user/dynamic',
          name: 'UserDynamic',
          component: UserDynamic
        },
        {// 用户的文章页面
          path: '/user/article',
          name: 'UserArticle',
          component: UserArticle
        },
        {// 用户的关注页面
          path: '/user/follow',
          name: 'UserFollow',
          component: UserFollow
        },
        {// 编辑个人资料
          path: '/user/edit',
          name: 'UserEdit',
          component: UserEdit
        },
        {// 关注的话题
          path: '/user/topic',
          name: 'UserTopic',
          component: UserTopic
        },
        {// 关注的问题
          path: '/user/problem',
          name: 'UserProblem',
          component: UserProblem
        },
        {// 关注了谁
          path: '/user/following',
          name: 'UserFollowing',
          component: UserFollowing
        }, {// 被谁关注
          path: '/user/followed',
          name: 'UserFollowed',
          component: UserFollowed
        },
        {// 写的文章
          path: '/user/article',
          name: 'UserArticle',
          component: UserArticle
        }
      ],
      redirect: '/user/dynamic'
    },
    {// 知乎通知页面
      path: '/notice',
      name: 'NoticePage',
      component: NoticePage,
      meta: {auth: true}
    },
    {// 知乎私信页面
      path: '/letter',
      name: 'PrivateLetterPage',
      component: PrivateLetterPage,
      meta: {auth: true}
    },
    {// 私信里面的 查看对话
      path: '/letter/letterview/:ids/:username',
      name: 'LetterView',
      component: LetterView,
      meta: {auth: true}
    },
    {// 首页里面的 推荐页面里的单个文章页面
      path: '/articlePage/:id',
      name: 'HomeArticlePage',
      component: HomeArticlePage,
      meta: {auth: true}
    },
    {// test测试页面
      path: '/test',
      name: 'Test',
      component: Test
    },
    {// 进入后台管理系统前操作页
      path: '/adminEnter',
      name: 'AdminEnter',
      component: AdminEnter,
      children: [
        {// 登入功能
          path: '/adminLogin',
          name: 'AdminLoginBox',
          component: AdminLoginBox
        },
        {// 注册功能
          path: '/adminRegister',
          name: 'AdminRegisterBox',
          component: AdminRegisterBox
        },
        {// 忘记密码功能
          path: '/adminForget',
          name: 'AdminForgetBox',
          component: AdminForgetBox
        }
      ],
      redirect: '/adminLogin'// 默认先显示登入功能
    },
    {// 进入后台管理系统功能首页结构
      path: '/adminHomes',
      name: 'AdminHomes',
      component: AdminHomes,
      meta: {auths: true},
      children: [
        {// 系统功能首页展示页
          path: '/adminHomes/home',
          name: 'AdminHomePage',
          component: AdminHomePage,
          meta: {auths: true}
        },
        {// 系统功能菜单1
          path: '/adminHomes/xxxPage1',
          name: 'xxxPage1',
          component: xxxPage1,
          meta: {auths: true},
          children: [
            {
              path: '/xxxPage1/function1',
              name: 'Function1',
              component: Function1,
              meta: {auths: true}
            }
          ],
          redirect: '/xxxPage1/function1'
        },
        {// 系统功能菜单2
          path: '/adminHomes/xxxPage2',
          name: 'xxxPage2',
          component: xxxPage2,
          meta: {auths: true},
          children: [
            {
              path: '/xxxPage2/function2',
              name: 'Function2',
              component: Function2,
              meta: {auths: true}
            }
          ],
          redirect: '/xxxPage2/function2'
        },
        {// 系统功能菜单3
          path: '/adminHomes/xxxPage3',
          name: 'xxxPage3',
          component: xxxPage3,
          meta: {auths: true}
        },
        {// 系统功能菜单4
          path: '/adminHomes/xxxPage4',
          name: 'xxxPage4',
          component: xxxPage4,
          meta: {auths: true}
        }
      ],
      redirect: '/adminHomes/home'// 默认进入先显示首页展示
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    return {
      x: 0,
      y: 0
    }
  }
})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.matched.some(m => m.meta.auth)) {
    // 对路由进行验证
    if (sessionStorage.getItem('isLogin') === '1') { // 已经登陆
      next()
    } else { // 未登录跳转到登陆界面
      next({ path: '/login' })
      // next() // 测试
    }
  } else {
    next()
  }
  if (to.matched.some(m => m.meta.auths)) {
    // 对路由进行验证
    if (sessionStorage.getItem('isAdminLogin') === '1') { // 已经登陆
      next()
    } else { // 未登录跳转到登陆界面
      next({ path: '/adminLogin' })
      // next() // 测试
    }
  } else {
    next()
  }
})
export default router
